package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ebook.service.dao.CategoryServiceDao;
import cn.ebook.service.dao.EbookServiceDao;
import cn.ebook.service.dao.impl.CategoryServiceImpl;
import cn.ebook.service.dao.impl.EbookServiceImpl;
import cn.ebook.util.PageBean;

public class EbookServlet extends HttpServlet {
	public EbookServiceDao es = new EbookServiceImpl();
	public CategoryServiceDao cs = new CategoryServiceImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		// 获取参数
		String nowstr = request.getParameter("nowPage");
		if ("".equals(nowstr) || nowstr == null) {
			nowstr = "1";
		}
		Integer now = new Integer(nowstr);
		Integer pageSize = 5;
		String categoryidstr = request.getParameter("categoryid");
		if("".equals(categoryidstr)|null==categoryidstr){
			categoryidstr="0";
		}
		Long categoryId = new Long(categoryidstr);
		// 调用接口
		PageBean pageBean = es.findByCategoryAndLimit(categoryId, now, pageSize);
		// 调用service图书类型的接口并放到request域中
		request.setAttribute("categorys", cs.findAllCategorys());
		
		request.setAttribute("categoryid", categoryId);
		// 把结果放到request的域中
		request.setAttribute("ebooks", pageBean);
		// 做出响应
		request.getRequestDispatcher("/index.jsp").forward(request, response);		
		return;
	}

}
